home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / lang / SmallEiffel.lha / SmallEiffel / lib_rand / min_stand.e < prev    next >
Text File  |  1998-12-22  |  2KB  |  88 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://www.loria.fr/SmallEiffel
  11. --
  12. class MIN_STAND
  13.    --
  14.    -- Implements the Minimal Standard generator from Press et. al.
  15.    -- Numerical Recipies.
  16.    --
  17.  
  18. inherit GEN_RAND;
  19.    
  20. creation make, with_seed
  21.  
  22. feature {NONE}
  23.  
  24.    ia: INTEGER is 16807;
  25.  
  26.    im: INTEGER is 2147483647;
  27.  
  28.    iq: INTEGER is 127773;
  29.  
  30.    ir: INTEGER is 2836;
  31.    
  32.    seed:INTEGER
  33.  
  34. feature {NONE}
  35.  
  36.    make is
  37.       local
  38.      seed_init: INTEGER;
  39.       do
  40.      seed_init := 1 + Current.to_pointer.hash_code;
  41.      from
  42.      until
  43.         seed_init < im
  44.      loop
  45.         seed_init := seed_init - iq;
  46.      end;
  47.      with_seed(seed_init);
  48.       end;
  49.  
  50.    with_seed(seed_value: INTEGER) is
  51.       require
  52.      valid_seed: seed_value > 0 and seed_value < im
  53.       do
  54.      seed:= seed_value;
  55.      next;
  56.       end;
  57.    
  58. feature
  59.  
  60.    next is
  61.       local
  62.      k: INTEGER;
  63.       do
  64.      k := seed // iq;
  65.      seed := ia * (seed -k * iq) - ir * k;
  66.      if seed < 0 then
  67.         seed := seed + im;
  68.      end;
  69.       end;
  70.  
  71.    last_real: REAL is 
  72.       do
  73.      Result := (seed/im).to_real;
  74.       end;
  75.  
  76.    last_integer(n:INTEGER): INTEGER is
  77.       do
  78.      Result := seed \\ n+1;
  79.       end;
  80.  
  81. invariant
  82.  
  83.    good_seed: seed > 0 and seed < im
  84.  
  85. end -- MIN_STAND
  86.  
  87.  
  88.